package framework;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * This class implements FrameworkInterface.
 * 
 */

public class FrameworkImpl implements FrameworkInterface {
	// Client-defined plugins
	AutomatonPluginInterface automaton;
	VisualizerPluginInterface visualizer;
	// Store all cells
	private List<CellInterface> cells;
	// Stores all selfdraw cells
	private List<SelfDrawCellInterface> selfDrawCells;

	/**
	 * Constructor that initializes fields, creates selfdraw cells for all
	 * cells, and create new Display
	 */
	public FrameworkImpl(VisualizerPluginInterface vp,
			AutomatonPluginInterface ap) {
		// Initialize plugins
		automaton = ap;
		visualizer = vp;
		// Create selfdraw cells
		SelfDrawCellInterface[] scs = createSelfDrawCells();
		// Create new Display
		new DisplayImpl(scs, ap, vp);
	}

	/**
	 * Gets cells from client-defined board and create selfdraw cells
	 * 
	 * @return SelfDrawInterface[] selfDrawCells - all the selfDrawCells
	 */
	private SelfDrawCellInterface[] createSelfDrawCells() {
		// Get board from client-defined automaton plugin
		BoardInterface b = automaton.getBoard();
		// Get cells from board
		cells = new ArrayList<CellInterface>(Arrays.asList(b.getCells()));
		// Create list to store selfdraw cells
		selfDrawCells = new ArrayList<SelfDrawCellInterface>();

		// For each cell, create a selfdraw cell from it. This uses the factory
		// method. The newly create selfdraw cell is the product.
		for (CellInterface c : cells) {

			selfDrawCells.add(new SelfDrawCell(c, visualizer, automaton));
		}

		// Convert list of selfdraw cells into array to facilitate future
		// computations
		return selfDrawCells.toArray(new SelfDrawCellInterface[selfDrawCells
				.size()]);
	}
}
